﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class System_User : ATPages
{
    ZSql info = new ZSql();
    Function f = new Function();
    static string sortcnd = " order by userid desc";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            HidModuId.Value = Request["moduid"].ToString();
            if (Request["deptid"] != null)
                hidDeptid.Value = Request["deptid"].ToString();

            Bind();
            lblPath.Text = f.getModuleidAdd(Request["moduid"]);
        }
    }


    //数据绑定
    private void Bind()
    {
        if (Request["pn"] == null)
            PageNavigator1.SetCurPage(1);
        else
            PageNavigator1.SetCurPage(Convert.ToInt16(Request["pn"].ToString()));

        string query = "select userid,username,realname,deptname,role,userbh,IP from userinfo a join department b on a.deptid = b.deptid join role c on a.roleid = c.roleid  where 1=1";

        if (Request["deptid"] == null)
        {
            PageNavigator1.Query = query + sortcnd;
        }
        else
        {
            PageNavigator1.Query = query + "and a.deptid = " + Request["deptid"].ToString() + sortcnd;
        }
    }


    //编辑
    protected void lnkbtnEdit_Click(object sender, EventArgs e)
    {
        string userid = (sender as LinkButton).CommandArgument;

        Response.Redirect("AddUser.aspx?uid=" + userid + "&deptid=" + hidDeptid.Value + "&moduid=" + HidModuId.Value);

        Response.End();
    }


    //删除
    protected void lnkbtnDel_Click(object sender, EventArgs e)
    {
        string userid = (sender as LinkButton).CommandArgument;
        if (userid != "1")
        {
            info.GetScalar("delete from userinfo where userid = " + userid);
            info.GetScalar("delete from ModulePurview where userid=" + userid);
            info.GetScalar("delete from UserPurview where userid=" + userid);
        }

        info.Close();
    }


    //protected void lnkbtnAdd_Click(object sender, EventArgs e)
    //{
    //    if (hidDeptid.Value == "")
    //        Response.Redirect("AddUser.aspx");
    //    else
    //        Response.Redirect("AddUser.aspx?deptid=" + hidDeptid.Value);

    //    Response.End();
    //}


    protected void lnkbtnSet_Click(object sender, EventArgs e)
    {
        string userid = (sender as LinkButton).CommandArgument;

        int pagenum = PageNavigator1.CurPage;

        string url = "PurviewSet.aspx?t=2&id=" + userid + "&pn=" + pagenum + "&moduid=" + HidModuId.Value;

        if (hidDeptid.Value == "")
            Response.Redirect(url);
        else
            Response.Redirect(url + "&deptid=" + hidDeptid.Value + "&moduid=" + HidModuId.Value);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string sql = "select userid,username,realname,deptname,role,userbh,IP from userinfo a join department b on a.deptid = b.deptid join role c on a.roleid = c.roleid where 1=1";
        if (!string.IsNullOrEmpty(txtContent.Text))
        {
            sql += "  and  a.userbh like '" + Common.FormatParameter(txtContent.Text) + "%'";
        }

        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (hidDeptid.Value == "")
            Response.Redirect("AddUser.aspx?moduid=" + HidModuId.Value);
        else
            Response.Redirect("AddUser.aspx?deptid=" + hidDeptid.Value + "&moduid=" + HidModuId.Value);

        Response.End();
    }
}
